package com.ideal.blockchain.controller.deploy;


import com.ideal.blockchain.deploy.Host;
import com.ideal.blockchain.dto.response.ResultInfo;
import com.ideal.blockchain.enums.ResponseCodeEnum;
import com.ideal.blockchain.service.deploy.HostServiceImpl;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/deploy")
public class HostDeploy {
    @Autowired
    private HostServiceImpl hostService;

    List<Host> existHost=new ArrayList<>();

    @RequestMapping("/hostList")
    @ResponseBody
    public List<Host> hostList(){
        System.out.println(hostService.queryAll().toString());

        for (int i = 0; i < hostService.queryAll().size(); i++) {
            existHost.add(hostService.queryAll().get(i));
        }
        HashSet tmp=new HashSet(existHost);
        existHost.clear();
        existHost.addAll(tmp);
        System.out.println("existHost"+existHost.toString());
        return hostService.queryAll();
    }

    @RequestMapping(value = "/addHost",method = RequestMethod.POST)
    public ResultInfo addHost(@RequestBody Host host) throws UnsupportedEncodingException {
        System.out.println("host = " + host);
        for (int i = 0; i < existHost.size(); i++) {
            if (host.getIp().equals(existHost.get(i).getIp())||host.getMark().equals(existHost.get(i).getMark())){
                return new ResultInfo(ResponseCodeEnum.FAILURE, "不能添加重复的mark或者ip");
            }
        }
        try{
            hostService.addHost(host);
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        String defaultCharsetName=Charset.defaultCharset().displayName();
        System.out.println("defaultCharsetName:"+defaultCharsetName);
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "添加主机成功");
    }

    @RequestMapping(value = "/alterHost",method = RequestMethod.POST)
    public ResultInfo alterHost(@RequestBody Host host){
        System.out.println("host = " + host);
        try {
            hostService.alterHost(host);
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "修改主机成功");
    }

    @RequestMapping(value = "/delHost",method = RequestMethod.POST)
    public ResultInfo delHost(@RequestBody  Host host){
        System.out.println("ip=:" + host.getIp());
        try {
            hostService.delHost(host.getIp());
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "删除主机成功");
    }
}
